﻿using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZoneLinePacking.Models.Entity.BaseInfo;
using ZoneLinePacking.Models.Entity.Template;
using ZoneLinePacking.Services.DB;
using ZoneLinePacking.Services.Message;
using ZoneLinePacking.ViewModelBase;

namespace ZoneLinePacking.ViewModels.Dialogs {

    public class BoxCodePrintDialogViewModel : DialogViewModel {

        public BoxCodePrintDialogViewModel(
            IRegionManager regionManager,
            IDialogService dialog,
            IEventAggregator eventAggregator,
            IMessageService message,
            IDbOperate<IFreeSqlMainContext> mainDbOperate)
            : base(regionManager, dialog, eventAggregator, message, mainDbOperate) {


        }

        private ObservableCollection<TemplateEntity> _boxCodeTemps = new ObservableCollection<TemplateEntity>();

        public ObservableCollection<TemplateEntity> BoxCodeTemps {
            get { return _boxCodeTemps; }
            set { _boxCodeTemps = value; base.RaisePropertyChanged(); }
        }

        private TemplateEntity _selectTemplateEntity;

        public TemplateEntity SelectTemplateEntity {
            get { return _selectTemplateEntity; }
            set { _selectTemplateEntity = value; base.RaisePropertyChanged(); }
        }


        private int _quantity = 1;

        public int Quantity {
            get { return _quantity; }
            set { _quantity = value; base.RaisePropertyChanged(); }
        }


        public DelegateCommand SaveCommand => new DelegateCommand(() => {
            if (_selectTemplateEntity == null) {
                _message.Warning("请选择打印模板！");
                return;
            }

            if (Quantity <= 0) {
                _message.Warning("数量必须大于1！");
                return;
            }

            DialogResult dialogResult = new DialogResult(ButtonResult.OK);
            var userInputList = _mainDbOperate.GetModelList<ParamsEntity>(o => o.TemplateID == SelectTemplateEntity.Id && o.ParamsType == Enums.eParamType.Input).ToList();
            var dic = userInputList.ToDictionary(o => o.ParamsKey, o => Quantity.ToString($"D{o.Length}"));
            dialogResult.Parameters.Add("TemplateEntity", SelectTemplateEntity);
            dialogResult.Parameters.Add("UserInputParams", dic);
            base.OnDialogClosed(dialogResult);
        });

        public DelegateCommand CancelCommand => new DelegateCommand(() => {
            base.OnDialogClosed(ButtonResult.Cancel);
        });


        protected override void OnViewLoaded() {
            var list =_mainDbOperate.GetModelList<TemplateEntity>(o => o.TemplateType == Enums.eTemplateType.BoxCode);
            BoxCodeTemps.Clear();
            BoxCodeTemps.AddRange(list);
            base.OnViewLoaded();
        }

    }

}
